package com.yanqu.road.server.logic.activity.condition.union;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

public class JoinUnionActivityCondition extends BaseActivityCondition {

    public JoinUnionActivityCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.JoinUnionActivityCondition.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.JoinUnionActivityCondition.getValue(), handle);
    }

    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        UserInfo userInfo = (UserInfo) object;
        if (null != userInfo) {
            if (!StringUtils.isNullOrEmpty(userInfo.getUnionUid())) {
                setValue(BigInteger.ONE);
            } else {
                setValue(BigInteger.ZERO);
            }
        }
    }

    @Override
    public void initValue() {
        UserInfo userInfo = player.getUserInfo();
        if (null != userInfo) {
            if (!StringUtils.isNullOrEmpty(userInfo.getUnionUid())) {
                setValue(BigInteger.ONE);
            } else {
                setValue(BigInteger.ZERO);
            }
        }
    }
}
